﻿using System;
using System.Data;
using System.Windows.Forms;

namespace AP_GestArm
{
    public partial class frmBitolas : Form
    {
        BaseDados _bitolas;
        public frmBitolas()
        {
            InitializeComponent();
            GridBitolas.CellFormatting += GridBitolas_CellFormatting;
            GridBitolas.CellValidating += GridBitolas_Validating;
            txtPesquisa.KeyDown += new KeyEventHandler(txtPesquisa_KeyDown);
        }

        private void frmBitolas_Load(object sender, EventArgs e)
        {
            _bitolas = new BaseDados(Application.StartupPath + "\\Data\\bd.mdb");
            BindingSource SourceBitolas = new BindingSource(_bitolas.d7, "Bitolas");
            GridBitolas.DataSource = SourceBitolas;
            GridBitolas.Columns[0].Visible = false;            
            GridBitolas.Sort(GridBitolas.Columns["Código"], System.ComponentModel.ListSortDirection.Ascending);

            GridBitolas.CurrentCell = null;
            GridBitolas.AllowUserToAddRows = false;
            foreach (DataGridViewRow oRow in GridBitolas.Rows)
            { oRow.Visible = false; }
        }

        private void txtPesquisa_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) toolStripButton1_Click(sender, e);
        }

        private void GridBitolas_Validating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            double newDouble;
            if (GridBitolas.Columns[e.ColumnIndex].Name == "Dimensao")            
                if (GridBitolas.Rows[e.RowIndex].Cells[e.ColumnIndex].IsInEditMode &&
                    !String.IsNullOrEmpty(e.FormattedValue.ToString()))
                {
                    btSave.Enabled = true;
                    if (!double.TryParse(e.FormattedValue.ToString(),
                        out newDouble) || newDouble < 0)
                    {
                        e.Cancel = true;
                        MessageBox.Show("Tem de inserir um número", "Erro");
                    }
                }            
        }

        private void GridBitolas_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex != 2) return;
            if (string.IsNullOrEmpty(e.Value.ToString())) return;
            
            double valor = Convert.ToDouble(e.Value);
            e.Value = valor.ToString("N2");
        }

        #region Nova Row

        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            DataRow newRow = _bitolas.d7.Tables["Bitolas"].NewRow();
            _bitolas.d7.Tables["Bitolas"].Rows.Add(newRow);

            GridBitolas.CurrentCell = null;
            foreach (DataGridViewRow oRow in GridBitolas.Rows)
            { oRow.Visible = false; }

            GridBitolas.Rows[0].Visible = true;
            GridBitolas.CurrentCell = GridBitolas.Rows[0].Cells["Código"];
            GridBitolas.BeginEdit(true);
            btSave.Enabled = true;
        }

        #endregion

        #region Grava row de Bitolas

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            Validate();
            GridBitolas.EndEdit();
            btSave.Enabled = false;

            try
            { _bitolas.da.Update(_bitolas.d7.Tables["Bitolas"]); }
            catch
            { }

            GridBitolas.CurrentCell = null;
            foreach (DataGridViewRow oRow in GridBitolas.Rows)
            { oRow.Visible = false; }
        }

        #endregion

        #region Pesquisa

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (txtPesquisa.Text.Trim() == "")
            {
                foreach (DataGridViewRow oRow in GridBitolas.Rows)
                { oRow.Visible = true; }
            }
            else
            {
                foreach (DataGridViewRow oRow in GridBitolas.Rows)
                {
                    if (string.Compare(txtPesquisa.Text.Trim(), oRow.Cells[1].Value.ToString().Trim(), true) == 0)
                    { oRow.Visible = true; }
                    else
                    {
                        GridBitolas.CurrentCell = null;
                        oRow.Visible = false;
                    }
                }
            }
        }

        #endregion

        # region Apaga Row de Bitolas

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (this.GridBitolas.CurrentCell != null)
            {
                try
                {
                    this.GridBitolas.Rows.RemoveAt(this.GridBitolas.CurrentRow.Index);
                    this.btSave.Enabled = true;
                }
                catch
                { }
            }
        }

        #endregion

        #region Limpa Grid

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow oRow in GridBitolas.Rows)
            {
                GridBitolas.CurrentCell = null;
                oRow.Visible = false;
            }
        }

        #endregion
    }


}